# delimit ;
clear ;
cd "replication" ;
set more off ;

* **************************************************************************** ;
* outcome: short-term health knowledge ; 
* **************************************************************************** ;

* re-run midline analysis ;
cd ".." ;
do "./replication/do/table03-midline-knowledge-aggregate.do" ; 

* generate interactions with education and gender ;
gen healthonlyXcompleted_elem = healthonly*completed_elem ;
gen healthandpayXcompleted_elem = healthandpay*completed_elem ;

gen healthonlyXfemale = healthonly*female ;
gen healthandpayXfemale = healthandpay*female ;

label var healthonlyXcompleted_elem "HEE $\ast$ Completed Elem" ;
label var healthandpayXcompleted_elem "HEEC $\ast$ Completed Elem" ;

label var healthonlyXfemale "HEE $\ast$ Woman" ;
label var healthandpayXfemale "HEEC $\ast$ Woman" ;

* regressions: interaction with completed educ ;
areg score_tot healthonly healthandpay completed_elem healthonlyXcompleted_elem healthandpayXcompleted_elem, a(strata) robust ;

qui est sto short_edu ; 
	
test healthonly = healthandpay ; 
estadd local titlerow1 = "" ; 
estadd scalar fstat1 = r(F): short_edu ; 
estadd scalar pval1 = r(p): short_edu ; 

test healthonly + healthonlyXcompleted_elem = healthandpay + healthandpayXcompleted_elem ;
estadd local titlerow2= "" ; 
estadd scalar fstat2 = r(F): short_edu ; 
estadd scalar pval2 = r(p): short_edu ; 

* regressions: interaction with woman ;
areg score_tot healthonly healthandpay healthonlyXfemale healthandpayXfemale, a(strata) robust ;

qui est sto short_fem ; 
	
test healthonly = healthandpay ; 
estadd local titlerow1 = "" ; 
estadd scalar fstat1 = r(F): short_fem ; 
estadd scalar pval1 = r(p): short_fem ; 

test healthonly + healthonlyXfemale = healthandpay + healthandpayXfemale ;
estadd local titlerow2 = "" ; 
estadd scalar fstat2 = r(F): short_fem ; 
estadd scalar pval2 = r(p): short_fem ; 

* **************************************************************************** ;
* outcome: longer-term health knowledge ; 
* **************************************************************************** ;

* re-run endline analysis ;
cd ".." ;
do "./replication/do/table04-endline-knowledge-aggregate.do" ; 

* generate interactions with education and gender ;
gen healthonlyXcompleted_elem = healthonly*completed_elem ;
gen healthandpayXcompleted_elem = healthandpay*completed_elem ;

gen healthonlyXfemale = healthonly*female ;
gen healthandpayXfemale = healthandpay*female ;

label var healthonlyXcompleted_elem "HEE $\ast$ Completed Elem" ;
label var healthandpayXcompleted_elem "HEEC $\ast$ Completed Elem" ;

label var healthonlyXfemale "HEE $\ast$ Woman" ;
label var healthandpayXfemale "HEEC $\ast$ Woman" ;

* regressions: interaction with completed educ ;
areg module_all healthonly healthandpay completed_elem healthonlyXcompleted_elem healthandpayXcompleted_elem, a(strata) robust ;

qui est sto longer_edu ; 
	
test healthonly = healthandpay ; 
estadd local titlerow1 = "" ; 
estadd scalar fstat1 = r(F): longer_edu ; 
estadd scalar pval1 = r(p): longer_edu ; 

test healthonly + healthonlyXcompleted_elem = healthandpay + healthandpayXcompleted_elem ;
estadd local titlerow2 = "" ; 
estadd scalar fstat2 = r(F): longer_edu ; 
estadd scalar pval2 = r(p): longer_edu ; 

* regressions: interaction with woman ;
areg module_all healthonly healthandpay healthonlyXfemale healthandpayXfemale, a(strata) robust ;

qui est sto longer_fem ; 
	
test healthonly = healthandpay ; 
estadd local titlerow1 = "" ; 
estadd scalar fstat1 = r(F): longer_fem ; 
estadd scalar pval1 = r(p): longer_fem ; 

test healthonly + healthonlyXfemale = healthandpay + healthandpayXfemale ;
estadd local titlerow2 = "" ; 
estadd scalar fstat2 = r(F): longer_fem ; 
estadd scalar pval2 = r(p): longer_fem ; 

* **************************************************************************** ;
* outcome: behavior
* **************************************************************************** ;

* re-run endline analysis ;
cd ".." ;
do "./replication/do/table05-endline-behavior.do" ; 

* generate interactions with education and gender ;
gen healthonlyXcompleted_elem = healthonly*completed_elem ;
gen healthandpayXcompleted_elem = healthandpay*completed_elem ;

gen healthonlyXfemale = healthonly*female ;
gen healthandpayXfemale = healthandpay*female ;

label var healthonlyXcompleted_elem "HEE $\ast$ Completed Elem" ;
label var healthandpayXcompleted_elem "HEEC $\ast$ Completed Elem" ;

label var healthonlyXfemale "HEE $\ast$ Woman" ;
label var healthandpayXfemale "HEEC $\ast$ Woman" ;

* regressions: interaction with completed educ ;
areg behav_all healthonly healthandpay completed_elem healthonlyXcompleted_elem healthandpayXcompleted_elem, a(strata) robust ;

qui est sto behav_edu ; 
	
test healthonly = healthandpay ; 
estadd local titlerow1 = "" ; 
estadd scalar fstat1 = r(F): behav_edu ; 
estadd scalar pval1 = r(p): behav_edu ; 

test healthonly + healthonlyXcompleted_elem = healthandpay + healthandpayXcompleted_elem ;
estadd local titlerow2 = "" ; 
estadd scalar fstat2 = r(F): behav_edu ; 
estadd scalar pval2 = r(p): behav_edu ; 
	
* regressions: interaction with woman ;
areg behav_all healthonly healthandpay healthonlyXfemale healthandpayXfemale, a(strata) robust ;

qui est sto behav_fem ; 
	
test healthonly = healthandpay ; 
estadd local titlerow1 = "" ; 
estadd scalar fstat1 = r(F): behav_fem ; 
estadd scalar pval1 = r(p): behav_fem ; 

test healthonly + healthonlyXfemale = healthandpay + healthandpayXfemale ;
estadd local titlerow2 = "" ; 
estadd scalar fstat2 = r(F): behav_fem ; 
estadd scalar pval2 = r(p): behav_fem ; 

* **************************************************************************** ;
* output regression results
* **************************************************************************** ;

* create the outcome variables again so that they are labelled in the LaTeX table ;
gen score_tot = . ;
label var score_tot "\shortstack[l]{Short-term \\ health\\ knowledge \\ score: \\ All topics}" ;

gen module_all = . ;
label var module_all "\shortstack[l]{Longer-term \\health \\knowledge\\ score: \\ All topics}" ;
label var behav_all "\shortstack[l]{Health \\behaviors: \\Total no. of\\ behaviors\\adopted}" ;

esttab short_edu longer_edu behav_edu short_fem longer_fem behav_fem using "./output/appendix-table-heterogeneity.tex",
	replace
	drop(_cons completed_elementary) 
	order(healthonly healthandpay healthonlyXcompleted_elem healthandpayXcompleted_elem healthonlyXfemale healthandpayXfemale) 
	cells(b(label() star fmt(%9.3f %9.3f)) se(par)) 
	star(* 0.10 ** 0.05 *** 0.01)  
	stats(titlerow1 fstat1 pval1 titlerow2 fstat2 pval2 N, fmt(%9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.0f) 
	labels("\(F\)-test, HEE = HEEC" 
	"$\qquad$ \(F\)-statistic" "$\qquad$ \(p\)-value"
	"\(F\)-test, HEE + HEE $\ast$ \textit{X} = HEEC + HEEC $\ast$ \textit{X}" 
	"$\qquad$ \(F\)-statistic" "$\qquad$ \(p\)-value"
	"Observations"))
	prehead(\begin{table}\centering \small `"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}"'
		\captionsetup{justification=centering} 
		\caption{Heterogeneity by Education and Gender}
		\label{table-hetero}
		\begin{tabular*}{\hsize}{p{7.5cm}p{2cm}p{2cm}p{2cm}p{2cm}p{2cm}p{2cm}}
		\toprule)
	legend label  booktabs  collabels( , none)
	mgroups(
	"Education"
	"Gender" 
	, pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	postfoot(`"\bottomrule"'  \end{tabular*} \captionsetup{justification=justified, width=\hsize} 
		\caption*{\footnotesize \textit{Notes:}
		\textit{HEE} is a dummy for assignment to only health entertainment-education. 
		\textit{HEEC} is a dummy for assignment to health entertainment-education with cash incentives for test performance. \
		\textit{Completed Elem} is a dummy equal to 1 for respondents who completed elementary school.
		\textit{Woman} is a dummy equal to 1 for women respondents. 
		Robust standard errors.
		All regressions include dummies for randomization strata, where strata are defined by gender, neighborhood, and microfinance client status. The regressions in columns 1--3 also include the \textit{Completed Elem} dummy. In the bottom panel of the table, \textit{HEE} $\ast$ \textit{X} refers to the interaction \textit{HEE} $\ast$ \textit{Completed Elem} for columns 1--3, and \textit{HEE} $\ast$ \textit{Woman} for columns 4--6. 
		***\$\,p < 0.01$, **\$\,p < 0.05$, *\$\,p<0.10$.
		}
		\end{table}) ;

exit ;


